/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jili.ubert;

import com.alibaba.fastjson.JSON;
import com.jili.ubert.code.server2client.Price;
import java.io.IOException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.avis.client.NotificationEvent;
import org.avis.client.NotificationListener;

/**
 *
 * @author ChengJiLi
 */
class AcceptPrice implements NotificationListener {

    private static final Log log = LogFactory.getLog(AcceptPrice.class);
    //private String regionID = null;
//    private OrderQuene orderQuene = null;
    private int runType = 1;

    public AcceptPrice(int runType) {
        this.runType = runType;
    }

    public AcceptPrice(String regionID) {
        //this.regionID = regionID;
//        this.orderQuene = ResourceManager.OrderQuenes;
    }

    public AcceptPrice(String regionID, int runType) {
        //this.regionID = regionID;
//        this.orderQuene = ResourceManager.OrderQuenes;
        this.runType = runType;
    }

    @Override
    public void notificationReceived(NotificationEvent ne) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.

        /*
         结构：Title=price,RegionID = "C",MaketCode = "SH",Data=price的protobuf流；
         */
        log.info("Title:" + ne.notification.getString("Title")
                + "  RegionID:" + ne.notification.getString("RegionID")
                + "  MaketCode:" + ne.notification.getString("MaketCode")
                + "  Data:" + ne.notification.get("Data"));
        String maketCode = ne.notification.getString("MaketCode");
        Price price = new Price();
        byte[] dd = ne.notification.getOpaque("Data");
        try {
            price = price.DeCode(dd);
            //保存最新行情
            log.info("收到行情Price："+JSON.toJSONString(price));
            ResourceManager.PriceQuenes.put(price.getObj() + "_" + maketCode, price);
            //匹配 成交
            log.info("OrderQuenes:"+JSON.toJSONString(ResourceManager.OrderQuenes));
            if (ResourceManager.OrderQuenes.containsKey(price.getObj() + "_" + maketCode)) {
                OrderQuene order = ResourceManager.OrderQuenes.get(price.getObj() + "_" + maketCode);
                //NewOrder o = null;
                //orderset.stream().filter(o->o.getOrderPrice()>=price.getLastPrice());
                ResourceManager.Match(order, price, maketCode, 1);
            }
        } catch (IOException ex) {
            log.error(ex);
        }
        //PriceQuenes;

    }
}
